package com.xinqi.modules.sys.sensitive.word.controller.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.green.utils.SensitiveWordUtils;
import com.xinqi.modules.sys.sensitive.word.dto.req.SensitiveWordCreateDTO;
import com.xinqi.modules.sys.sensitive.word.dto.req.SensitiveWordQueryDTO;
import com.xinqi.modules.sys.sensitive.word.dto.req.SensitiveWordUpdateDTO;
import com.xinqi.modules.sys.sensitive.word.dto.rsp.SensitiveWordResultDTO;
import com.xinqi.modules.sys.sensitive.word.service.SensitiveWordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 敏感词管理表WEB控制层
 *
 * @author: wenjie
 * @date: 2023/08/02
 */
@Tag(name = "Web-敏感词管理表", description = "敏感词管理表Web接口")
@RestController
@RequestMapping("/web/sensitive-word")
@RequiredArgsConstructor
public class SensitiveWordWebController {
    private final SensitiveWordService sensitiveWordService;

    /**
     * 敏感词管理表-分页
     */
    @Operation(summary = "敏感词管理表-分页")
    @GetMapping("/page")
    public R<PageInfo<SensitiveWordResultDTO>> page(@ParameterObject SensitiveWordQueryDTO dto) {
        PageInfo<SensitiveWordResultDTO> page = sensitiveWordService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 敏感词管理表-列表
     */
    @Operation(summary = "敏感词管理表-列表")
    @GetMapping("/list")
    public R<List<SensitiveWordResultDTO>> list(@ParameterObject SensitiveWordQueryDTO dto) {
        List<SensitiveWordResultDTO> list = sensitiveWordService.find(dto);
        return R.data(list);
    }

    /**
     * 敏感词管理表-详情
     */
    @Operation(summary = "敏感词管理表-详情")
    @GetMapping("/{id}")
    public R<SensitiveWordResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        SensitiveWordResultDTO dto = sensitiveWordService.findById(id);
        return R.data(dto);
    }

    @Operation(summary = "敏感词管理表-根据分类和是否启用查询详情")
    @GetMapping("/find-by-type-and-status")
    public R<SensitiveWordResultDTO> findByTypeAndStatus(@Parameter(description = "type", example = "涉恐") String type, @Parameter(description = "status", example = "1") Integer status) {
        SensitiveWordResultDTO resultDTO = sensitiveWordService.findByTypeAndStatus(type, status);
        return R.data(resultDTO);
    }

    /**
     * 新增敏感词管理表
     */
    @Operation(summary = "新增敏感词管理表")
    @PostMapping
    public R<Long> insert(@RequestBody SensitiveWordCreateDTO dto) {
        return R.data(sensitiveWordService.insert(dto));
    }

    /**
     * 修改敏感词管理表
     */
    @Operation(summary = "修改敏感词管理表")
    @PutMapping("/{id}")
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody SensitiveWordUpdateDTO dto) {
        dto.setId(id);
        return R.data(sensitiveWordService.updateById(dto));
    }

    /**
     * 删除敏感词管理表
     */
    @Operation(summary = "删除敏感词管理表")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(sensitiveWordService.deleteById(id));
    }

    /**
     * 获取redis中的字典值
     */
    @Operation(summary = "获取redis中的字典值")
    @GetMapping("/check-sensitive-word")
    public R<Boolean> checkSensitiveWord(String text) {
        return R.data(SensitiveWordUtils.checkSensitiveWord(text));
    }
}
